import React, { PureComponent } from 'react'

export default class App extends PureComponent {
  constructor() {
    super();
    this.state = {
      username: "",
      password: "",
      valid: "",
    }
  }
  render() {
    return (
      <div>
        <form onSubmit={e => this.handleSubmit(e)}>

          <div>
            <label htmlFor="">
              用户：
             {/* 受控组件 */}
              <input id="username"
                name="username"
                type="text"
                onChange={e => this.handleChange(e)}
                value={this.state.username} />

            </label>
          </div>

          <div>
            <label htmlFor="">
              密码：
             {/* 受控组件 */}
              <input id="password"
                name="password"
                type="text"
                onChange={e => this.handleChange(e)}
                value={this.state.password} />
            </label>
          </div>

          <div>
            <label htmlFor="">
              验证：
             {/* 受控组件 */}
              <input id="valid"
                name="valid"
                type="text"
                onChange={e => this.handleChange(e)}
                value={this.state.valid} />
            </label>
          </div>

          <input type="submit" value="提交" />
        </form>
      </div>
    )
  }
  handleSubmit(event) {
    event.preventDefault();

    const { username, password, valid } = this.state
    console.log(username, password, valid);
  }
  handleChange(event) {
    console.log(event.target.name);
    this.setState({
      // 计算属性名
      [event.target.name]: event.target.value
    })
  }
}
